<template>
  <view>
    <!-- 搜索框 -->
    <uni-search-bar placeholder="搜索" :radius="100" cancelButton="none" @input="search" v-model="keywords" :focus="true"></uni-search-bar>
    <!-- 搜索历史 -->
    <view class="historyList" v-show="keywords === ''">
      <view class="title">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="17" @click.native="clear"></uni-icons>
      </view>
      <view class="list">
        <block v-if="historyList.length > 0">
          <view class="item" v-for="(it,i) in historyList" :key="i" @click="search(it)">
            {{it}}
          </view>
        </block>
      </view>
    </view>
    <!-- 搜索列表 -->
    <view class="subList" @click="goDetail">
      <view class="item" v-for="it in searchList" :key="it.goods_id" :data-id="it.goods_id">
        <text :data-id="it.goods_id" class="text">{{it.goods_name}}</text>
        <uni-icons :data-id="it.goods_id" type="right" size="16"></uni-icons>
      </view>
    </view>
  </view>
</template>

<script>
  import {searchGoods} from '../../utils/api.js'
  export default {
    data() {
      return {
        keywords:"",
        timer:null,
        searchList:[],
        historyList:[],
        keyMap:{}
      };
    },
    methods:{
      // 搜索框输入事件回调
      search(name){
        if(name !== undefined) {
          this.keywords = name
        }
        if(this.keywords === '') {
          this.searchList = []
          return
        }
        // console.log(this.keywords);
        // 防抖
        clearTimeout(this.timer)
        this.timer = setTimeout(async()=>{
          // 搜集搜索历史
          if(!this.keyMap[this.keywords]){
            this.keyMap[this.keywords] = true
            this.historyList.unshift(this.keywords)
          }
          // 获取搜索数据
          await this.searchGoods()
          // 清除定时器
          clearTimeout(this.timer)
          this.timer = null
        },500)
      },
      // 获取搜索数据
      async searchGoods() {
        try{
          const {data} = await searchGoods({query:this.keywords})
          // console.log(data);
          this.searchList = data.message
        }catch(e){
          //TODO handle the exception
        }
      },
      // 跳转详情页
      goDetail(e){
        const id = e.target.dataset.id
        if(id !== undefined) {
          // console.log(id);
          uni.navigateTo({
            url:'/subpkg/good_detail/good_detail?goods_id=' + id
          })
          // 清空搜索列表
          this.searchList = []
          this.keywords = ''
        }
      },
      // 清空历史记录
      clear() {
        this.historyList = []
      }
    }
  }
</script>

<style lang="scss">
  .historyList {
    .title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding:10px;
      font-size: 13px;
      border-bottom: 1px solid #ccc;
    }
    .list {
      display: flex;
      flex-wrap: wrap;
      .item {
        box-sizing: border-box;
        margin: 0 5px;
        padding: 0 5px;
        background-color: #e0e0e0;
      }
    }
  }
  .subList {
    padding: 0 5px;
    .item {
      display: flex;
      padding: 13px 0;
      font-size: 12px;
      border-bottom:1px solid #efefef;
      justify-content: space-between;
      align-items: center;
      .text {
        // 文本一行显示
        white-space: nowrap;
        // 溢出隐藏
        overflow: hidden;
        // 溢出文本省略号显示
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
</style>
